<?xml version="1.0" encoding="utf-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  <xsl:template match="Album">

  <svg width="300mm" height="260mm" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
      <path id="textCurve" d="M20,70 H450
                              M35,100 H450
                              M35,130 H450
                              M35,160 H450"/>
      <path id="textCurve2" d="M20,70 H400"/>
      <path id="textBack" d="M35,0 H450
                             M35,30 H450
                             M35,60 H450
                             M35,90 H450"/>
      <path id="textBackSmall" d="M35,0 H550
                                  M35,30 H450
                                  M35,60 H450
                                  M35,90 H450"/>
      <path id="textCover" d="M140,30 H380
                              M140,60 H380
                              M140,90 H450
                              M140,160 H120"/>
    </defs>

    <xsl:variable name="trackCount" select="count(Track)"/>
    <xsl:variable name="trackFontSize">
      <xsl:choose>
        <xsl:when test="$trackCount>18">
          <xsl:value-of select="9.5"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="12"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="trackSpace">
      <xsl:choose>
        <xsl:when test="$trackCount>18">
          <xsl:value-of select="2.7"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="3.5"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <g id="front" transform="translate(20, 20)">
      <rect x="0mm" y="0mm" width="120mm" height="120mm" fill="none" stroke="black" stroke-width=".2mm"/>
      <g id="row_big">
        <rect y="60mm" width="20mm" height="20mm" rx="5mm" fill="#3465a4"/>
        <rect x="{1 * 24}mm" y="60mm" width="20mm" height="20mm" rx="5mm" fill="#75507b"/>
        <rect x="{2 * 24}mm" y="60mm" width="20mm" height="20mm" rx="5mm" fill="#8ae234"/>
        <rect x="{3 * 24}mm" y="60mm" width="20mm" height="20mm" rx="5mm" fill="#cc0000"/>
        <rect x="{4 * 24}mm" y="60mm" width="20mm" height="20mm" rx="5mm" fill="#edd400"/>
      </g>

      <text x="60mm" y="16mm" font-family="arial" font-size="30" font-weight="bold" style="baseline-shift: -15;">
        <textPath xlink:href="#textCurve">
          <xsl:value-of select="Name"/>
        </textPath>
      </text>
    </g>

    <g id="booklet" transform="translate(512, 20)">
      <rect x="0mm" y="0mm" width="120mm" height="120mm" fill="none" stroke="black" stroke-width=".2mm"/>

      <rect x="30mm" y="40mm" width="8mm" height="8mm" rx="1mm" fill="#3465a4"/>
      <rect x="{1 * 11 + 30}mm" y="40mm" width="8mm" height="8mm" rx="1mm" fill="#75507b"/>
      <rect x="{2 * 11 + 30}mm" y="40mm" width="8mm" height="8mm" rx="1mm" fill="#8ae234"/>
      <rect x="{3 * 11 + 30}mm" y="40mm" width="8mm" height="8mm" rx="1mm" fill="#cc0000"/>
      <rect x="{4 * 11 + 30}mm" y="40mm" width="8mm" height="8mm" rx="1mm" fill="#edd400"/>

      <text x="60mm" y="10mm" font-family="arial" font-size="30" font-weight="bold" style="baseline-shift: -15;">
        <textPath xlink:href="#textCurve">
          <xsl:value-of select="Name"/>
        </textPath>
      </text>

      <xsl:for-each select="Track">
       <xsl:sort select="TrackNumber" data-type="number"/>
        <text x="5mm" y="{position()*$trackSpace+30}mm" font-size="{$trackFontSize}" font-family="arial" style="baseline-shift: -15;">
          <textPath xlink:href="#textCurve2">
            <xsl:variable name="tracknumber"><xsl:value-of select="TrackNumber"/></xsl:variable>
            <xsl:if test="$tracknumber!=''">
              <xsl:value-of select="format-number (TrackNumber, '#00')"/>.
              <tspan xml:space="preserve" font-weight="bold">composed by </tspan><xsl:value-of select="Composer"/>,
              <xsl:value-of select="Year"/>
            </xsl:if>
          </textPath>
        </text>
      </xsl:for-each>
    </g>

    <g id="back" transform="translate(512, 512)">

      <rect x="0mm" y="0mm" width="151mm" height="118mm" fill="none" stroke="black" stroke-width=".2mm"/>
      <rect x="0mm" y="0mm" width="6.5mm" height="118mm" fill="none" stroke="black" stroke-width=".2mm"/>
      <rect x="144.5mm" y="00mm" width="6.5mm" height="118mm" fill="none" stroke="black" stroke-width=".2mm"/>

      <text transform="rotate(-90) translate(-278, 17)" font-family="arial" font-size="12" writing-mode="tb" font-weight="bold" style="text-anchor: middle; baseline-shift: -15;">
        <xsl:value-of select="Name"/>
      </text>

      <text x="90mm" y="10mm" font-family="arial" font-size="30" font-weight="bold" style="baseline-shift: -15;">
        <textPath xlink:href="#textBack">
          <xsl:value-of select="Name"/>
        </textPath>
      </text>

      <g id="row_small">
        <rect x="1mm" y="1mm" width="4mm" height="4mm" rx="1mm" fill="#3465a4"/>
        <rect x="1mm" y="{1 * 6 + 1}mm" width="4mm" height="4mm" rx="1mm" fill="#75507b"/>
        <rect x="1mm" y="{2 * 6 + 1}mm" width="4mm" height="4mm" rx="1mm" fill="#8ae234"/>
        <rect x="1mm" y="{3 * 6 + 1}mm" width="4mm" height="4mm" rx="1mm" fill="#cc0000"/>
        <rect x="1mm" y="{4 * 6 + 1}mm" width="4mm" height="4mm" rx="1mm" fill="#edd400"/>
      </g>

      <use xlink:href="#row_small" x="145mm" y="88mm"/>


      <text transform="rotate(90) translate(169, -555)" font-family="arial" font-size="12" writing-mode="tb" font-weight="bold" style="text-anchor: middle; baseline-shift: -15;">
        <xsl:value-of select="Name"/>
      </text>

      <use xlink:href="#row_big" x="25mm" y="35mm"/>

      <xsl:for-each select="Track">
       <xsl:sort select="TrackNumber" data-type="number"/>
        <text x="50mm" y="{position()*$trackSpace+20}mm" font-size="{$trackFontSize}" font-family="arial" style="baseline-shift: -15;">
        <textPath xlink:href="#textBackSmall">
          <xsl:variable name="tracknumber"><xsl:value-of select="TrackNumber"/></xsl:variable>
          <xsl:if test="$tracknumber!=''">
            <tspan xml:space="preserve" font-weight="bold"><xsl:value-of select="format-number (TrackNumber, '#00')"/>. </tspan>
            <xsl:value-of select="Name"/> 
            <tspan xml:space="preserve">  </tspan>
            (<xsl:value-of select="floor(number(TotalTime) div 1000 div 60)"/>
            <xsl:value-of select="format-number (floor(number(TotalTime) div 1000 mod 60), ':#00')"/>)
          </xsl:if>
        </textPath>
        </text>
      </xsl:for-each>

    </g>

    <g id="cd" transform="translate(0, 520)">
      <ellipse cx="65mm" cy="60mm" rx="60mm" ry="60mm" stroke="#000000" stroke-width=".2mm" fill="#ffffff"/>
      <ellipse cx="65mm" cy="60mm" rx="59mm" ry="59mm" stroke="#3465a4" stroke-width="2mm" fill="#ffffff"/>
      <ellipse cx="65mm" cy="60mm" rx="9mm" ry="9mm" stroke="#8ae234" stroke-width="2mm" fill="#ffffff"/>
      <ellipse cx="65mm" cy="60mm" rx="8mm" ry="8mm" stroke="#000000" stroke-width=".2mm" fill="#ffffff"/>

      <text x="65mm" y="10mm" font-family="arial" font-size="20" font-weight="bold" style="baseline-shift: -15;">
        <textPath xlink:href="#textCover">
          <xsl:value-of select="Name"/>
        </textPath>
      </text>

      <xsl:if test="Track/DiscNumber != '' and Track/DiscCount != ''">
        <text x="100mm" y="66mm" font-family="arial" font-size="84" fill="#a7a7a7" font-weight="bold" style="baseline-shift: -15; text-anchor: middle;">
          <xsl:value-of select="Track/DiscNumber"/>/<xsl:value-of select="Track/DiscCount"/>
        </text>
      </xsl:if>

      <text x="33mm" y="93mm" font-size="14" font-family="arial" style="baseline-shift: -15;">
        Genre: <xsl:value-of select="Track/Genre"/>
      </text>

      <use xlink:href="#row_small" x="25mm" y="65mm"/>
      <use xlink:href="#row_small" x="30mm" y="42mm"/>
      <use xlink:href="#row_small" x="20mm" y="27mm"/>
      <use xlink:href="#row_small" x="27mm" y="13mm"/>

    </g>

  </svg>

  </xsl:template>
</xsl:transform>
